<?php
/**
 * Created by PhpStorm.
 * Author: WuDean
 * Date: 2018/11/9 0009
 * Time: 11:15
 * Name: 网易云 易盾通信
 */
namespace App\Api\Helper;
class YiDunSMS {

    private $secretId;//密钥ID
    private $secretKey;//私有密钥
    private $businessId;//业务ID
    private $apiUrl = "https://sms.dun.163yun.com/v2/sendsms";//发送验证码接口

    const SMS_TYPE = ['validate','notice','marketing'];
    /**
     * YiDunSMS constructor.
     * @param $businessId
     * @name 构造数据
     */
    public function __construct($businessId)
    {
        $this->secretId = config('wangyi.secretId');
        $this->secretKey = config('wangyi.secretKey');
        $this->businessId = $businessId;
    }


    /**
     * @param $templateId
     * @param array $mobiles
     * @param string $code
     * @return mixed
     * @name 发送短信
     * @user IHammer
     */
    public function sendSms($templateId, $mobiles,$code = '')
    {
        $params = array(
            'secretId' => $this->secretId,
            'businessId' =>  $this->businessId,
            'version' => 'v2',
            'timestamp' => date('YmdHis',time()),
            'nonce' => $this->getRandom(32),
            'mobile' => $mobiles,
            'params' => 'code='.$code,
            'templateId' => $templateId,
            'needUp' => 'true',
        );
        $signature = $this->genSignature($this->secretKey,$params);
        $params['signature'] = $signature;
        return self::postUrlReturn($this->apiUrl,$params,false);
    }

    /**
     * @param $url
     * @param null $data
     * @param bool|false $json
     * @return array|mixed
     * @name post
     * @user IHammer
     */
    public static function postUrlReturn($url, $data = NULL, $json = false){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            if($json && is_array($data)){
                $data = json_encode( $data );
            }
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            if($json){//发送JSON数据
                curl_setopt($curl, CURLOPT_HEADER, 0);
                curl_setopt($curl, CURLOPT_HTTPHEADER,
                    array(
                        'Content-Type: application/json; charset=utf-8',
                        'Content-Length:' . strlen($data)));
            }
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        $errorno = curl_errno($curl);
        if ($errorno) {
            return array('errorno' => false, 'errmsg' => $errorno);
        }
        curl_close($curl);
        return json_decode($res, true);
    }

    /**
     * @param $secretKey 产品私钥
     * @param $params 接口请求参数，不包括signature参数
     * @return string
     * @name 生成签名信息
     * @user IHammer
     */
    public function genSignature($secretKey,$params){
        ksort($params);
        $buff="";
        foreach($params as $key=>$value){
            $buff .=$key;
            $buff .=$value;
        }
        $buff .= $secretKey;
        return md5(mb_convert_encoding($buff, "utf8", "auto"));
    }

    /**
     * @param int $num
     * @return string
     * @name 随机生成字符串
     * @user IHammer
     */
    public function getRandom($num=32){
        $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $key = "";
        for($i=0;$i<$num;$i++)
        {
            $key .= $str{mt_rand(0,32)};    //生成php随机数
        }
        return $key;
    }

}